package com.jiuzhou.guanwang.jzcp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.URLConfig;
import com.jiuzhou.guanwang.jzcp.activity.HomeActivity;
import com.jiuzhou.guanwang.jzcp.activity.LastOpenActivity;
import com.jiuzhou.guanwang.jzcp.adapter.HomeSpecialityAdapter;
import com.jiuzhou.guanwang.jzcp.base.DefaultAdapter;
import com.jiuzhou.guanwang.jzcp.bean.HomeCPBean;
import com.jiuzhou.guanwang.jzcp.loader.GlideImageLoader;
import com.jiuzhou.guanwang.jzcp.netutil.NetRequestUtil;
import com.jiuzhou.guanwang.jzcp.netutil.response.CaiPiaoListResponse;
import com.jiuzhou.guanwang.jzcp.widget.RiseNumberTextView;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HomeFragment extends Fragment{

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    HomeSpecialityAdapter homeSpecialityAdapter;
    @BindView(R.id.tv_red_1)
    RiseNumberTextView tvRed1;
    @BindView(R.id.tv_red_2)
    RiseNumberTextView tvRed2;
    @BindView(R.id.tv_red_3)
    RiseNumberTextView tvRed3;
    @BindView(R.id.tv_red_4)
    RiseNumberTextView tvRed4;
    @BindView(R.id.tv_red_5)
    RiseNumberTextView tvRed5;
    @BindView(R.id.tv_red_6)
    RiseNumberTextView tvRed6;
    @BindView(R.id.tv_blue)
    RiseNumberTextView tvBlue;
    @BindView(R.id.iv_refresh)
    ImageView ivRefresh;
    private View rootView;
    private Unbinder unbinder;
    private ArrayList<Integer> bannerList = new ArrayList<>();
    private ArrayList<HomeCPBean> homeCPBeans = new ArrayList<>();
    private ArrayList<Integer> redNums = new ArrayList<>();
    private ArrayList<Integer> blueNums = new ArrayList<>();


    public static HomeFragment newInstance(String info) {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        args.putString("info", info);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            String name = bundle.get("info").toString();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_home, null);
            unbinder = ButterKnife.bind(this, rootView);
            initView();
            initData();
        } else {
            // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，
            // 要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }

        return rootView;
    }

    private void initView() {
        bannerList.add(R.drawable.banner1);
        bannerList.add(R.drawable.banner2);
        bannerList.add(R.drawable.banner3);
        bannerList.add(R.drawable.banner4);
        banner.setImages(bannerList)
                .setImageLoader(new GlideImageLoader())
                .start();

        initHotCp();
        //幸运号码
        refreshNum();
    }

    private void initHotCp() {


        //特色基金列表
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setFocusable(false);
        recyclerView.setLayoutManager(layoutManager);
        homeSpecialityAdapter = new HomeSpecialityAdapter(getActivity(), homeCPBeans);
        recyclerView.setAdapter(homeSpecialityAdapter);
        homeSpecialityAdapter.setOnItemClickListener(new DefaultAdapter.OnItemClickListener<HomeCPBean>() {
            @Override
            public void onClick(View view, RecyclerView.ViewHolder holder, HomeCPBean item, int position) {

                Intent intent = new Intent(getActivity(), LastOpenActivity.class);
                intent.putExtra("name", item.getName());
                intent.putExtra("code",item.getCode());
                startActivity(intent);
            }
        });
    }

    private void initData() {
        NetRequestUtil.getInstance().post(URLConfig.CAIPIAO_LIST, null, 100, new NetRequestUtil.NetResponseListener<String>() {
            @Override
            public void onSuccess(String response, int requestCode) {
                CaiPiaoListResponse caiPiaoListResponse = new Gson().fromJson(response, CaiPiaoListResponse.class);
                List<HomeCPBean> result = caiPiaoListResponse.getShowapi_res_body().getResult();
                for (int i = 0; i < result.size(); i++) {
                    if (result.get(i).getCode().equals("qlc") || result.get(i).getCode().equals("qxc") ||
                            result.get(i).getCode().equals("dlt") || result.get(i).getCode().equals("ssq")) {
                        homeCPBeans.add(result.get(i));
                    }
                }
                Collections.reverse(homeCPBeans);
                homeSpecialityAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFailed(String response, int requestCode) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.tv_blue, R.id.iv_refresh,R.id.rl_zq,R.id.rl_lq})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_blue:
                break;
            case R.id.iv_refresh:
                refreshNum();
                break;
            case R.id.rl_zq:
                ((HomeActivity)getActivity()).setTabSelected(3);
                break;
            case R.id.rl_lq:
                ((HomeActivity)getActivity()).setTabSelected(3);
                break;
        }
    }

    private void refreshNum() {
        randomSSQ();
        for (int i = 0; i < redNums.size(); i++) {
            switch (i) {
                case 0:
                    tvRed1.withNumber(redNums.get(i));
                    tvRed1.setDuration(500);
                    tvRed1.start();
                    break;
                case 1:
                    tvRed2.withNumber(redNums.get(i));
                    tvRed2.setDuration(500);
                    tvRed2.start();
                    break;
                case 2:
                    tvRed3.withNumber(redNums.get(i));
                    tvRed3.setDuration(500);
                    tvRed3.start();
                    break;
                case 3:
                    tvRed4.withNumber(redNums.get(i));
                    tvRed4.setDuration(500);
                    tvRed4.start();
                    break;
                case 4:
                    tvRed5.withNumber(redNums.get(i));
                    tvRed5.setDuration(500);
                    tvRed5.start();
                    break;
                case 5:
                    tvRed6.withNumber(redNums.get(i));
                    tvRed6.setDuration(500);
                    tvRed6.start();
                    break;
            }


        }

        tvBlue.withNumber(blueNums.get(0));
        tvBlue.setDuration(500);
        tvBlue.start();
    }

    //机选一注
    private void randomSSQ() {
        Random random = new Random();
        redNums.clear();
        //机选红球
        while (redNums.size() <= 6) {
            int num = random.nextInt(33) + 1;
            if (redNums.contains(num)) {
                continue;
            }
            redNums.add(num);
        }
        Collections.sort(redNums);

        //机选蓝球
        blueNums.clear();
        int num = random.nextInt(16) + 1;
        blueNums.add(num);
    }

}
